﻿using System;
using System.Linq;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.HCLoad.Utils;

namespace YArchitech.HCLoad.Export
{
	public class HcXmlBuilding : HcXmlElement
	{
		public HcXmlBuilding(HcDataManager dataManager) : base(dataManager)
		{
			HcXmlBuilding.nConstructorIndex++;
		}

		public string GetIdStr()
		{
			return "bldg-" + HcXmlBuilding.nConstructorIndex.ToString();
		}

		public override bool WriteElementData(ref XmlElement elem_Buidling)
		{
			if (base.DataManager.Levels.Count<Level>() == 0)
			{
				return false;
			}
			bool result = base.WriteElementData(ref elem_Buidling);
			XmlDocument ownerDocument = elem_Buidling.OwnerDocument;
			elem_Buidling.SetAttribute("id", this.GetIdStr());
			foreach (Level level in base.DataManager.Levels)
			{
				XmlElement xmlElement = ownerDocument.CreateElement("BuildingStorey");
				elem_Buidling.AppendChild(xmlElement);
				xmlElement.SetAttribute("id", level.Id.ToString());
				HcXmlElement.AppendTextElement(ref xmlElement, "Name", level.Name);
				HcXmlElement.AppendTextElement(ref xmlElement, "Level", HcUnit.LengthConverFromAPI(base.DataManager.Document, level.Elevation).ToString());
			}
			if (base.DataManager.HcSpaces != null)
			{
				foreach (HcSpaceData spaceData in base.DataManager.HcSpaces)
				{
					XmlElement newChild = ownerDocument.CreateElement("Space");
					if (new HcXmlSpace(spaceData).WriteElementData(ref newChild))
					{
						elem_Buidling.AppendChild(newChild);
					}
				}
			}
			return result;
		}

		private static int nConstructorIndex;
	}
}
